package com.fact.actions;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.EntrySetDTO;
import com.fact.dto.NodeHierarchyDTO;
import com.fact.dto.ScenarioDTO;
import com.fact.models.EntrySetService;
import com.fact.models.NodeHierarchyService;
import com.fact.models.ScenarioService;
import com.fact.utils.AppConstant;

public class EntrySet extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	
	private Map<String,Object> session;
	private String page=AppConstant.ENTRY_SET;
	private String pageTitle="FMtool - Entry Set";
	private String opType;
	NodeAttributeService nodeAttributeService=new NodeAttributeService();
	EntrySetService entrySetService=new EntrySetService();
	private int year;
	private int month;
	private ArrayList<EntrySetDTO> entrySetList=new  ArrayList<EntrySetDTO>();
	private EntrySetDTO entrySet;
	private ArrayList<EntrySetDTO> entryList;
	
	NodeHierarchyService nhService=new NodeHierarchyService();
	ScenarioService scenarioService=new ScenarioService();
	EntrySetService enrySetService=new EntrySetService();
	
	public String entrySetHome()
	{
		return SUCCESS;
	}
	public String entrySetList()
	{
		entrySetList=entrySetService.getEntrySetList(month, year);
		return SUCCESS;
	}
	
	public String createEntrySet()
	{
		HttpServletResponse response = ServletActionContext.getResponse();
		ArrayList<NodeHierarchyDTO> nodeList=nhService.getKpiNodeList(entrySet.getSegmentId());
		ArrayList<ScenarioDTO> scenarioList=scenarioService.getScenarioList();
		boolean res=enrySetService.createEntrySet(entrySet, nodeList, scenarioList);
		
		String responseString="";
		if(res==true)
			responseString="Success";
		else
			responseString="Fail";
		
		try{
        	response.setContentType("text/xml");
        	response.setHeader("Cache-Control", "no-cache");
        	response.getWriter().write(responseString);
        	response.flushBuffer();
          }
        catch(Exception e) {e.printStackTrace();}
        
		return null;
	}
	
	public String createBatchEntrySet()
	{
		
		ArrayList<ScenarioDTO> scenarioList=scenarioService.getScenarioList();
		
		boolean response=entrySetService.createBatchEntrySet(entryList,scenarioList);
		
		if(response==false)
			addActionMessage("Error in Running Batch Process.");
		else
			this.session.put("operation_message", "Batch Process Successfully been executed.");

		return SUCCESS;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public String getPageTitle() {
		return pageTitle;
	}
	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}
	public String getOpType() {
		return opType;
	}
	public void setOpType(String opType) {
		this.opType = opType;
	}
	
	
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	
	public ArrayList<EntrySetDTO> getEntrySetList() {
		return entrySetList;
	}
	public void setEntrySetList(ArrayList<EntrySetDTO> entrySetList) {
		this.entrySetList = entrySetList;
	}
	
	
	public ArrayList<EntrySetDTO> getEntryList() {
		return entryList;
	}
	public void setEntryList(ArrayList<EntrySetDTO> entryList) {
		this.entryList = entryList;
	}
	public EntrySetDTO getEntrySet() {
		return entrySet;
	}
	public void setEntrySet(EntrySetDTO entrySet) {
		this.entrySet = entrySet;
	}
	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}

}
